/**
 *@author ZY
 *@date 2022/4/9 13:59
 *@Description:公共查询方法
 */
const utilsTools = require("../utils/utils.tools");
const db = require("../models/index");
const logger = require("../utils/utils.logger");

//整理统一返回格式
function resExtra(data, code = 200, message = '操作成功！') {
	return { data, code, message }
}

//查询列表条件处理
function queryConditions(conditions, count) {
	let queryCon = {
		where: {},
		// limit:500,
		// offset:0,
	}

	if (conditions.params) {
		queryCon.where = utilsTools.deleteNullObj(conditions.params)
	}
	//每页条数
	if (conditions.pageSize) {
		queryCon.limit = parseInt(conditions.pageSize)
	}
	//当前页
	if (conditions.pageNum || conditions.pageNum === 0) {
		queryCon.offset = conditions.pageSize * (conditions.pageNum - 1) || 0
	}
	if (conditions.sort) {
		queryCon.order = [[conditions.sort ? conditions.sort.prop : 'createdAt', conditions.sort ? conditions.sort.order : 'asc']] //默认按插入时间进行升序
	}
	if (conditions.order) {
		queryCon.order = conditions.order
	}
	if (conditions.exclude && !count) {
		queryCon.attributes = { exclude: [conditions.exclude] }
	}
	if (conditions.raw && !count) {
		queryCon.raw = true
	}
	if (conditions.include && !count) {
		queryCon.include = conditions.include
	}
	if (conditions.attributes) {
		queryCon.attributes = conditions.attributes
	}
	if (conditions.group) {
		queryCon.group = conditions.group
	}
	return queryCon
}
//***
const sqlOpt = {
	/**
	 * 查询数据总条数
	 * @param  {Object}   model       模型实例
	 * @param  {Object}   conditions  条件集合
	 * @param  {Function} cb          回调函数
	 */
	count: (model, conditions) => {
		try {
			return new Promise(async (resolve, reject) => {
				if (!model) {
					reject('模型不存在')
				}
				const data = await model.findAndCountAll(queryConditions(conditions, 'count'))
				resolve(resExtra(data.count))
			})
		} catch (error) {
			reject(error)
		}
	},

	/**
	 * 查询所有数据
	 * @param  {Object}   model       模型实例
	 * @param  {Object}   conditions  条件集合
	 */
	list: (model, conditions, count = 'count') => {
		/*查询条件格式
		conditions = {
				params: {
						title: ''
				},
				pageSize: 20,
				pageNum: 0,
				sort: {
						prop:'createdAt',
						order:'desc / asc：升序',
				}
		}*/

		return new Promise(async (resolve, reject) => {
			try {
				if (!model) {
					reject('模型不存在');
				}
				const condition = queryConditions(conditions, count)
				const countAll = await model.findAndCountAll(condition)
				const data = await model.findAll(condition)
				resolve(resExtra({
					data,
					count: countAll.count,
					pageNum: conditions.pageNum || 1,
					pageSize: conditions.pageSize || 10
				}))
			} catch (error) {
				reject(error)
			}
		})
	},

	/**
	 * 更具主键 获取一条数据
	 * @param  {Object}   model       模型实例
	 * @param  {Object}   conditions  条件集合
	 * @param  {Function} cb          回调函数
	 */
	findOne: (model, conditions, cb) => {
		return new Promise(async (resolve, reject) => {
			try {
				if (!model) {
					reject('模型不存在');
				}
				const data = await model.findOne(queryConditions(conditions))
				resolve(resExtra(data))
			} catch (error) {
				reject(error)
			}
		})
	},
}

module.exports = sqlOpt
